package com.example.roledemo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true,securedEnabled = true,prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("cxyxj")
                .password("123").roles("admin", "user")
                .and()
                .withUser("security")
                .password("security").roles("user");
    }

    @Override
   protected void configure(HttpSecurity http) throws Exception {
        http.formLogin();
        http
                .authorizeRequests()
                .antMatchers("/permitAll").permitAll()
                .anyRequest().authenticated()
               ;  // 访问/permitAll路径，不需要登录;
        http.csrf().disable();
/*        http.authorizeRequests()  //开启配置
                .antMatchers("/cxyxj/**").hasRole("admin")   //访问/cxyxj/**下的路径，必须具备admin身份
                .antMatchers("/security/**").hasRole("user") //访问/security/**下的路径，必须具备user身份
                .antMatchers("/permitAll").permitAll()  // 访问/permitAll路径，不需要登录
                .anyRequest() //其他请求
                .authenticated()//验证   表示其他请求只需要登录就能访问
                .and()
                .formLogin();*/

        /* http.authorizeRequests()  //开启配置
                //.antMatchers("/cxyxj/**").hasAuthority("admin")   //访问/cxyxj/**下的路径，必须具备admin身份
              //  .antMatchers("/security/**").hasRole("user") //访问/security/**下的路径，必须具备user身份
             //   .antMatchers("/permitAll").permitAll()  // 访问/permitAll路径，不需要登录
               // .mvcMatchers("/cxyxj/**").servletPath("/role").permitAll()
               // .antMatchers("/role/cxyxj/**").permitAll()
              //  .antMatchers("/admin/demo").hasIpAddress("192.168.64.5")
              //  .antMatchers("/cxyxj/**").access("hasAuthority('admin')")
               // .antMatchers("/permitAll").access("isAnonymous")
              //  .anyRequest().access("@myAccess.hasPermit(request,authentication)")
                .anyRequest() //其他请求
                .authenticated()//验证   表示其他请求只需要登录就能访问
                .and()
                .formLogin();*/
    }

}
